package com.game.action;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.game.game.Game;
import com.game.server.Action;
import com.game.server.GameThread;
import com.game.server.NetworkService;
import com.game.server.SocketThread;
import com.game.share.IOPackage;
import com.game.share.StreamWriter;

public class GetGameListAction implements Action {

	@Override
	public IOPackage execute(SocketThread st, String jsonString) {
		GameThread gameThread = NetworkService.get().getGameThread();
		ArrayList<Game> list = gameThread.getList();
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		StreamWriter writer = new StreamWriter(out);
		try {
			writer.writeInt(list.size());
			for (Game game : list) {
				writer.writeInt(game.getGameID());
				writer.writeString(game.getOwner().getName());
				writer.writeInt(game.getUserCount());
				writer.writeInt(game.getMaxUserCount());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return new IOPackage(IOPackage.PackageType.RESPONSE, out.toByteArray());
	}

}
